package com.sdtv.basemodule.http;

import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.sdtv.basemodule.config.AppConfig;
import com.sdtv.basemodule.config.AppHttpSetting;
import com.sdtv.basemodule.utils.EmptyUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.http
 * @ClassName: AppHttpClient
 * @Description: java类作用描述
 * @Author: 小郭同学
 * @CreateDate: 2019-06-25 17:01
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class AppHttpClient<T> {

    public volatile static AppHttpClient instance;

    private Retrofit retrofit;
    //retrofit使用的OKhttpclient
    private OkHttpClient okHttpClient;
    // 设置http请求的URL
    private String url;

    /**
     * 重新设置AppClient
     *
     * @param url
     * @return
     */
    public static AppHttpClient resetUrl(String url) {
        instance = null;
        if (null == instance) {
            synchronized (AppHttpClient.class) {
                if (null == instance) {
                    instance = new AppHttpClient(url);
                }
            }
        }
        return instance;
    }


    public static AppHttpClient getInstance() {
        if (null == instance) {
            synchronized (AppHttpClient.class) {
                if (null == instance) {
                    instance = new AppHttpClient();
                }
            }
        }
        return instance;
    }

    /**
     * 实例化service
     *
     * @param clazz
     * @return
     */
    public T initService(Class<T> clazz) {
        return retrofit.create(clazz);
    }

    public AppHttpClient() {
        AppHttpSetting httpSetting = AppConfig.INSTANCE.getHttpSetting();
        this.okHttpClient = httpSetting.getClientBuilder().build();
        String baseUrl = httpSetting.getUrl();
        if (!TextUtils.isEmpty(url)) {
            baseUrl = url;
        }
        this.retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .build();
    }

    public AppHttpClient(String url) {
        AppHttpSetting httpSetting = AppConfig.INSTANCE.getHttpSetting();
        this.okHttpClient = httpSetting.getClientBuilder().build();
        if (EmptyUtils.isNotEmpty(url)) {

        } else {
            this.retrofit = new Retrofit.Builder()
                    .baseUrl(url)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
        }

    }

    public OkHttpClient getOkHttpClient() {

        return okHttpClient;
    }

}
